/**
 * 站点共用JS
 */

$(function() {
	/**
	 * 头部搜索JS
	 */
	var timeId = null;
	var keyIndex = 0;
	var keyText = ["粘贴：你想购买的淘宝宝贝名称", "或粘贴：淘宝宝贝网址(http://item.taobao...id=7002269933)", "搜索一下，哇！ 还额外返利10元哦！ (^-^)"];
	
	/// 开启
	function openKeyText() {
		if ( ! isSysKeyText()) {
			clearTimeout(timeId);
			return ;
		}
		
		///
		$("#sTxt").val(keyText[keyIndex++]);
		if (keyIndex >= keyText.length) {
			keyIndex = 0;
		}
		
		///
		timeId = setTimeout(openKeyText, 3000);
	}
	
	/// 检测
	function isSysKeyText() {
		var flag = false;
		var tmp = $("#sTxt").val();
		if (tmp != "") {
			for (i=0; i<keyText.length; i++) {
				if (tmp == keyText[i]) {
					flag = true;
				}
			}
		} else {
			flag = true;
		}
		
		return flag;
	}
	
	///
	openKeyText();
	
	/// 点击“标签”
	$("#Search .tab a").click(function() {
		$("#Search .tab a").removeClass("on");
		$(this).addClass("on");
		
		$("#sHidden").val($(this).attr("type"));
	});
	
	/// 点击“文本框”
	$("#sTxt").focus(function() {
		clearTimeout(timeId);
		if (isSysKeyText()) {
			$("#sTxt").val("");
		}
	}).blur(function() {
		openKeyText();
	});
	
	/// 表单提交
	$("#searchForm").submit(function() {
		var keyword = $("#sTxt").val();
		if (isSysKeyText()) {
			return false;
		}
		return true;
	});
	
	/**
	 * 收藏
	 */
	$("#btnFavorite").click(function() {
		var company = "易购GO_淘宝网女装新款_淘宝网商城_淘宝网网上购物";
		var url = "http://www.e35go.com";
		if (document.all) {
			window.external.addFavorite(url, company);
		} else if (window.sidebar) {
			window.sidebar.addPanel(company, url, "");
		}
		return false;
	});
});


/**
 * 取得返现
 */
function cosCommission(commission) {
	
}

/**
 * 取得某个商品的转换链接
 * // iIds => 可以是单个值或一个数组
 * // callback => 回调函数
 */
function getTaobaoke(iIds, callback) {
	TOP.api('rest', 'get', 
		{
			 method: 'taobao.taobaoke.widget.items.convert',
			 fields: 'commission,volume,item_location,click_url',
			 num_iids: iIds
		}, function(resp) {
			var list = [];
			if (resp.error_response) {
				/// 出错
				alert('taobao.taobaoke.widget.items.convert接口获取商信息品失败!'+resp.error_response.msg);
			} else {
				list = resp.taobaoke_items.taobaoke_item;
			}
			callback(list);
		}
	);
}
